<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Material extends Model
{
    use HasFactory;

    protected $fillable = ['title','project_id','type_id','user_id','price', 'specs', 'unit', 'sums', 'brand', 'remarks', 'type','files'];

    const POSTONLY = ['title','project_id','type_id','user_id','price', 'specs', 'unit', 'sums', 'brand', 'remarks', 'type','files'];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function contract()
    {
        return $this->hasOne(MaterialContract::class,'material_id','id')->with('application')->select('id','b_name','material_id','b_mobile','b_agent','b_agent_mobile','tax_rate');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function payment_apply()
    {
        return $this->hasOne(PaymentApply::class,'source_id','id')->select('id','apply_date','source_id','source_type')->where('source_type','material');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','Material')->orderBy('id','desc');
    }

    public function applicationPay()
    {
        return $this->hasMany(Application::class,'source_id','id')->where('model','MaterialPay')->where('status','approved')->orderBy('id','desc');
    }

}
